<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Created by PhpStorm.
 * Date: 2018/12/25
 * Author: Zhangzhenbei <zhenbeigg@163.com>
 */
namespace app\api\controller;
use think\Db;
use think\Request;
class Index extends Common
{
    /**
    *首页
    *@access  public
    */
    public function index()
    {
        //轮播图
        $lunbo = Db::name('sys_ad')->where(['category_id'=>1,'status'=>0])->field('img_url,xlink_url')->select();
        $lunbo = edit_img_url($lunbo,'img_url',1);
        $nav = Db::name('sys_ad')->where(['category_id'=>2,'status'=>0])->limit(8)->field('id,img_url,xlink_url')->select();
        $nav = edit_img_url($nav,'img_url',1);
        $couponImg = Db::name('sys_ad')->where(['category_id'=>14,'status'=>0])->field('id,img_url,xlink_url')->select();
        $couponImg = edit_img_url($couponImg,'img_url',1);
        $goodsRec = $this->getGoodsData(false,true,false,'','id,title,img_url,sell_price,market_price');
        $goodsRec = edit_img_url($goodsRec,'img_url',1);
        $data['qrcode_url'] = sysconf('wx_qrcode');
        $data = edit_img_url($data,'qrcode_url',2);
        $data['lunbo'] = $lunbo;
        $data['nav'] = $nav;
        $data['couponImg'] = $couponImg;
        $data['goodsRec'] = $goodsRec;
        json_msg_data(0,'ok',$data);
    }
    /**
    *搜索
    *@access  public
    *@param  string $page 分页数
    *@param  string $keywords 搜索关键词
    */
    public function search()
    {
        $where = ['status'=>0];
        $per_page = 10;
        $page = input('param.page');
        if(!$page){
            json_msg(1, '请求错误');
        }
        $page = $page-1;
        $keywords = input('param.keywords');
        $keywords ? $where['title'] = ['like',"%{$keywords}%"] : '';
        $list = Db::name('goods')->where($where)->order('id desc')->limit($per_page*$page,$per_page)->field('id,title,img_url,sell_price,market_price')->select();
 
        if($list){
            $list = edit_img_url($list,'img_url',$type=1);
            json_msg_data(0,'ok',$list);
        }else{
            json_msg(1, '没有更多数据');
        }
    }

    public function test()
    {
        $list = Db::name('user')->select();
        foreach ($list as $k => $v) {
            if (empty($v['user_nickname']))
            {
                Db::name('user')->where('id',$v['id'])->update(['user_nickname'=>$v['user_name']]);
            }
        }
    }
    /**
    *搜索
    *@access  public
    */

    public function activity()
    {
        $list = Db::name('article')->where('category_id',15)->field('id,img_url,title')->select();
        $list = edit_img_url($list,'img_url',$type=1);
        json_msg_data(0,'ok',$list);
    }
}